﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.SystemSettings.QualificationAdministration;
using SystemCenterSoa.IRepository.SystemSettings.QualificationAdministration;
using SystemCenterSoa.PoModel.SystemSettings.QualificationAdministration;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemSettings.QualificationAdministration
{
    public class DictionaryTableRepository : BaseRepository, IDictionaryTableRepository
    {
        public DictionaryTableRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {

        }

        public bool DictionaryTableAdd(DictionaryTablePo info)
        {
            _systemCenterDbSoaContext.DictionaryTablePo.Add(info);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public List<DictionaryTablePo> DictionaryTableByTypeKey(string typeKey)
        {
            var list = _systemCenterDbSoaContext.DictionaryTablePo.Where(p => p.dt_type_key.Equals(typeKey));
            return list.ToList();
        }

        public bool DictionaryTableDelete(int id)
        {
            var list = _systemCenterDbSoaContext.DictionaryTablePo.Find(id);
            _systemCenterDbSoaContext.DictionaryTablePo.Remove(list);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public DictionaryTablePo DictionaryTableEdit(int id)
        {
            return _systemCenterDbSoaContext.DictionaryTablePo.FirstOrDefault(p => p.id == id);
        }

        public PagerResult<DictionaryTableDto> SearchPage(SearchModel searchModel)
        {
            var query = from a in _systemCenterDbSoaContext.DictionaryTablePo
                        join b in _systemCenterDbSoaContext.DictionaryTypeTablePo
                        on a.dt_type_key equals b.dt_type_key
                        select new DictionaryTableDto
                        {
                            creator_date = a.creator_date,
                            creator_name = a.creator_name,
                            dt_key = a.dt_key,
                            dt_name = a.dt_name,
                            dt_orderby = a.dt_orderby,
                            dt_status = a.dt_status,
                            dt_type_key = a.dt_type_key,
                            dt_type_name = b.dt_type_name,
                            id = a.id,
                            modifi_date = a.modifi_date,
                            modifi_name = a.modifi_name
                        };


            //判断有没有搜索条件
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                //把搜索条件拿出来
                var tempDi = searchModel.SearchItem;

                //搜索字段为role_name
                string name = "name";
                //判断搜索条件中是否有role_name
                if (tempDi.Keys.Contains(name) && !string.IsNullOrEmpty(tempDi[name]))
                {
                    query = query.Where(c => c.dt_name.Contains(tempDi[name]));
                }

                string type = "type";
                if (tempDi.Keys.Contains(type) && !string.IsNullOrEmpty(tempDi[type]))
                {
                    query = query.Where(c => c.dt_type_key.Equals(tempDi[type]));
                }

            }
            return BasePageSearch(query, searchModel);
        }

        public bool DictionaryTableUpdate(DictionaryTablePo info)
        {
            _systemCenterDbSoaContext.Entry(info).State = EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }

        public List<DictionaryTypeTablePo> BindType()
        {
            return _systemCenterDbSoaContext.DictionaryTypeTablePo.ToList();
        }
    }
}
